Skip to content

Nicer formatting of fallback, allow HTML code to be changed from 500#129

Merged
rwb27 merged 5 commits intomainfrom
reformat-fallback
Jul 7, 2025
Merged

Nicer formatting of fallback, allow HTML code to be changed from 500#129
rwb27 merged 5 commits intomainfrom
reformat-fallback

Conversation

@JohemianKnapsody
Copy link
Copy Markdown
Collaborator

Style changes:

  • Remove raw tag from error message, allowing the Python exception \n character to create a new line, improving readability
  • Add text wrapping to <pre> tag, preventing horizontal scroll bars

image

image

Changing status code to 206 (partial success) reading about 206, it's partial success if the client only requests partial, which doesn't apply here

Code to 218 (this is fine) until someone finds a better one

Reason is that Connect won't open a tab returning a 5xx message, so we can't see the fallback messages in Connect. This will also require getting rid of Connect's version checking that we're connecting with a v2 microscope...

Before

image

After

image

@barecheck
Copy link
Copy Markdown

barecheck bot commented Jul 2, 2025

Barecheck - Code coverage report

Total: 89.19%

Your code coverage diff: 0.01% ▴

Uncovered files and lines
FileLines
src/labthings_fastapi/server/fallback.py80

@JohemianKnapsody JohemianKnapsody changed the title Nicer formatting of fallback, returns 206 (partial) instead of 500 Nicer formatting of fallback, returns 218 (for now) instead of 500 Jul 2, 2025
@rwb27
Copy link
Copy Markdown
Collaborator

rwb27 commented Jul 2, 2025

I think the improved format is great. I am less convinced by the code: I think this might be better handled by updating Connect to open a page to display 5xx error messages.

In the long term we should consider changing the way Connect tests for microscopes so it doesn't rely on the v1 API (unless @julianstirling did that already), and perhaps adding an explicit test for LabThings fallback servers so they can show up as broken microscopes.

I assume the dead microscope only shows up if you enter the address manually?

@julianstirling
Copy link
Copy Markdown
Contributor

I was going to agree it was the wrong code until I read this

HTTP response status code 218 This is fine is sent by Apache to indicate that there is a HTTP client error or HTTP server error, but this special status code communicating success is used to maintain a standardized look and feel by the client.

It seems like it was invented exactly for this use where you want to notify there is an error but want the client to act like everything is fine and display something. I thing re-defining how electron handles 5xx errors seems like a can of worms, and if Apache felt the need for this code to exist we can't be the only ones.

Somewhat I think 5xx is the wrong error anyway, because while there is an internal server error, it isn't like it is a full 500 error which means that the server was unable to give you a helpful response. We have a helpful response with HTML content, it is just unfortunately helpful response that things are broken.

@julianstirling
Copy link
Copy Markdown
Contributor

julianstirling commented Jul 3, 2025

I personally would merge this now. But I won't while @rwb27 has reservations.

One option would be to make setting the code an option so that 500 can be the default given by labthings, but instead downstream can customise.

All that would be needed is (I can't suggest changes to unchanged lines in GitHub):

add to line 15:

        self.html_code = 500

as 74 would change to

    return HTMLResponse(content=content, status_code=app.html_code)

This way it becomes an OpenFlexure question of if 218 makes sense.

@julianstirling
Copy link
Copy Markdown
Contributor

I have added the change to make the code stable after discussing with Joe. This way LabThings doesn't have to be opinionated about the error code.

Copy link
Copy Markdown
Contributor

@julianstirling julianstirling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is ready to go.

I have made the HTML code 500 by standard, but configurable and moved the <style> block into the <head> block.

@julianstirling julianstirling added this to the v0.0.10 milestone Jul 6, 2025
@julianstirling julianstirling changed the title Nicer formatting of fallback, returns 218 (for now) instead of 500 Nicer formatting of fallback, allow HTML code to be changed from 500 Jul 7, 2025
Copy link
Copy Markdown
Collaborator

@rwb27 rwb27 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me.

@rwb27
Copy link
Copy Markdown
Collaborator

rwb27 commented Jul 7, 2025

I don't have a strong feeling for 218 vs 500 - my reservation is just that it could hide errors or confuse things, and that I have previously used HTTP codes I had just found and then decided it was non-standard. The 218 code does sound like it's exactly what we are looking for here (there's an error, but we want the client to render it like a page) so I think it is reasonable.

Julian's suggestion to move the 218 code into openflexure-microscope-server seems like a good resolution. I'm happy to merge this now.

@rwb27 rwb27 merged commit aa3b545 into main Jul 7, 2025
14 checks passed
@rwb27 rwb27 deleted the reformat-fallback branch July 7, 2025 10:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants